////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.menu {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	
	import kiwi.utils.Validation;
	
	import plantool.core.CoreApi;
	import plantool.events.MenuEvent;
	import plantool.modules.menu.data.MenuGroupData;
	import plantool.modules.menu.data.MenuItemData;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="group_added", type="plantool.events.MenuEvent")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class MenuApi extends CoreApi {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private static var _instance:MenuApi			= null;
		private var _groups:Vector.<MenuGroupData>		= new Vector.<MenuGroupData>();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get groups():Vector.<MenuGroupData> {
			return _groups;
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function MenuApi(pvt:PrivateConstructorClass) {
			//create main group
//			var mainGrp:MenuGroupData 	= new MenuGroupData();
//			mainGrp.title				= "Main Menu";
//			mainGrp.addItem(new MenuItemData(""));
//			addGroup(mainGrp);
			
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		public static function getInstance():MenuApi {
			//check if instance exists, if not create one
			if (!Validation.isSet(_instance)) {
				_instance = new MenuApi(new PrivateConstructorClass());
			}
			
			//return instance
			return _instance;
		}
		
		public function addGroup(grp:MenuGroupData):void {
			//validate group
			if (Validation.isSet(grp)) {
				//store group
				_groups.push(grp);
				
				//inform listeners of new group
				dispatchEvent(new MenuEvent(MenuEvent.GROUP_ADDED, grp));
			}
		}
		
		public override function install():void {
			//nothing to install, were just ready
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}

//--------------------------------------------------------------------------
//END OF PACKAGE
//Define the PrivateConstructorClass here so an instance cannot be by anyone
//except for the class defined in the above package
//--------------------------------------------------------------------------
class PrivateConstructorClass {
	//--------------------------------------------------------------------------
	//Constructor and Destructor
	//--------------------------------------------------------------------------
	public function PrivateConstructorClass() {
	}
}